package com.bianmaba.example.eacache;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.event.CacheManagerEventListener;
import net.sf.ehcache.event.CacheManagerEventListenerFactory;
import net.sf.ehcache.util.PropertyUtil;

import java.util.Properties;

/**
 * @program: example-web-framework-extends
 * @description:
 * @author: Chenjiabin
 * @create: 2018-04-21 18:17
 **/
public class DefaultCacheManagerEventListenerFactory extends CacheManagerEventListenerFactory {
    @Override
    public CacheManagerEventListener createCacheManagerEventListener(CacheManager cacheManager, Properties properties) {

        boolean enabled = isEnabled(properties);
        return new DefaultCacheManagerEventListener(cacheManager, enabled);
    }

    protected boolean isEnabled(Properties properties) {
        String replicatePutsString = PropertyUtil.extractAndLogProperty("enabled", properties);
        boolean replicatePuts;
        if (replicatePutsString != null) {
            replicatePuts = PropertyUtil.parseBoolean(replicatePutsString);
        } else {
            replicatePuts = true;
        }

        return replicatePuts;
    }
}